package com.sinoglobal.shakeprize.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sinoglobal.shakeprize.R;
import com.sinoglobal.shakeprize.bean.PrizeAddressDetails;

public class AddressListAdapter extends BaseAdapter {
	private Context context;
	private List<PrizeAddressDetails> data = new ArrayList<PrizeAddressDetails>();
	private PrizeAddressDetails checkedItem;
	
	public AddressListAdapter(Context context) {
		this.context = context;
	}

	public AddressListAdapter(Context context, List<PrizeAddressDetails> data) {
		this.context = context;
		this.data = data;
	}

	public void setData(List<PrizeAddressDetails> data) {
		this.data = data;
		notifyDataSetChanged();
	}
	
	/**
	 * @return 返回选中的position
	 */
	public PrizeAddressDetails getCheckedPosition() {
		return checkedItem;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate(context,
					R.layout.shake_prize_address_list_item, null);
			holder = new ViewHolder();
			holder.rbPrizeSelected = (RadioButton) convertView
					.findViewById(R.id.rbPrizeSelected);
			holder.tvRecipientName = (TextView) convertView
					.findViewById(R.id.tvRecipientName);
			holder.tvRecipientPhone = (TextView) convertView
					.findViewById(R.id.tvRecipientPhone);
			holder.tvDefaultAddress = (TextView) convertView
					.findViewById(R.id.tvDefaultAddress);
			holder.tvPrizeAddress = (TextView) convertView
					.findViewById(R.id.tvPrizeAddress);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if ("1".equals(data.get(position).is_default)) {
			holder.tvDefaultAddress.setVisibility(View.VISIBLE);
		} else {
			holder.tvDefaultAddress.setVisibility(View.INVISIBLE);
		}
		if (data.get(position).isChecked) {
			holder.rbPrizeSelected.setChecked(true);
			checkedItem=data.get(position);
		} else {
			holder.rbPrizeSelected.setChecked(false);
		}
		holder.tvRecipientName.setText(data.get(position).connect_name);
		holder.tvRecipientPhone.setText(data.get(position).connect_tel);
		holder.tvPrizeAddress.setText(data.get(position).area
				+ data.get(position).address);
		holder.rbPrizeSelected
				.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						for (int i = 0; i < data.size(); i++) {
							if (i == position) {
								data.get(i).isChecked = true;
								checkedItem=data.get(i);
							} else {
								data.get(i).isChecked = false;
							}
						}
						notifyDataSetChanged();

						
					}
				});
		return convertView;
	}

	static class ViewHolder {
		RadioButton rbPrizeSelected;
		TextView tvRecipientName;
		TextView tvRecipientPhone;
		TextView tvDefaultAddress;
		TextView tvPrizeAddress;
	}

}
